package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.common.AuditStatus;
import com.daxia.bumall.common.Logs;
import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.DesignerDTO;
import com.daxia.bumall.model.Designer;
import com.daxia.bumall.model.User;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.SpringSecurityUtils;

@Repository
public class DesignerDAO extends GenericDAOHibernate<Designer> {

    public List<Designer> find(DesignerDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From Designer n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (StringUtils.isNotBlank(dto.getCompany())) {
            hql.append(" and n.company like ? ");
            paras.add("%" + dto.getCompany() + "%");
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
        		hql.append(" and n.user.id = ? ");
        		paras.add(dto.getUser().getId());
        	} else {
        		if (dto.getUser().getSex() != null) {
        			hql.append(" and n.user.sex = ? ");
        			paras.add(dto.getUser().getSex());
        		}
        		if (dto.getUser().getCity() != null) {
        			hql.append(" and n.user.city.id = ? ");
        			paras.add(dto.getUser().getCity().getId());
        		}
        	}
        	
        }
        if (dto.getCity() != null) {
        	hql.append(" and n.user.city.id = ? ");
			paras.add(dto.getCity().getId());
        }
        if(dto.getCompanyUser()!=null){
        	hql.append(" and n.companyUser.id = ? ");
        	paras.add(dto.getCompanyUser().getId());
        }
        if (StringUtils.isNotBlank(dto.getSearchTerm())) {
        	hql.append(" and (n.user.username like ? or"
        			+ " n.user.nickName like ? "
        			+ " or n.user.realName like ?"
        			+ " or n.user.mobile like ?"
        			+ " or n.user.province.name like ? "
        			+ " or n.user.city.name like ? "
        			+ " or n.user.district.name like ? "
        			+ " or n.company like ? "
        			+ " or n.auditRemark like ? ) ");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        }
        
        if(StringUtils.isNotBlank(dto.getGoodAtStyles())){
        	hql.append(" and n.goodAtStyles like ? ");
        	paras.add("%"+dto.getGoodAtStyles()+"%");
        }
        
        if (dto.getAuditStatus() != null) {
        	hql.append(" and n.auditStatus = ? ");
        	paras.add(dto.getAuditStatus());
        }
        if (dto.isAdmin()) {
        	Logs.COMMON.debug("dto.getCityIdArr(): " + dto.getCityIdArr());
        	hql.append(" and n.city.id not in " ).append(dto.getCityIdArr()).append(" or n.city.id is null  ");
        }
        if(null!=dto.getRecommend()&&dto.getRecommend()!=-1){
        	hql.append(" and n.recommend = ? ");
        	paras.add(dto.getRecommend());
        }
        
      //排序规则
        if(null!=dto.getOrderType())
	        switch(dto.getOrderType()){
		        case Subscribe:
		        	hql.append(" order by n.dealCount desc ");
		        	break;
		        default :
		        	hql.append(" order by n.id desc ");	
	        }
        else
        	hql.append(" order by n.id desc ");	
        
        Logs.COMMON.debug("designer: " + hql);
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public Designer findOne(DesignerDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<Designer> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
    
    @Override
    public Long create(Designer t) {
    	if (t.getAuditStatus() == null) {
    		t.setAuditStatus(AuditStatus.NotAudit.getValue());
    	}
    	if (t.getCreator() == null && SpringSecurityUtils.getCurrentUser() != null) {
    		t.setCreator(new User(SpringSecurityUtils.getCurrentUser().getId()));
    	}
    	if (t.getAuditStatus() == null) {
    		t.setAuditStatus(AuditStatus.NotAudit.getValue());
    	}
    	if (t.getRecommend() == null) {
    		t.setRecommend(0);
    	}
    	return super.create(t);
    }
}
